package com.jeesite.modules.home.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.annotation.RepeatSubmit;
import com.jeesite.modules.common.service.SysOfficeUnitService;
import com.jeesite.modules.home.entity.DcSupMsgDto;
import com.jeesite.modules.home.entity.DcSupWarningDto;
import com.jeesite.modules.home.entity.EchartsDto;
import com.jeesite.modules.home.entity.cuibanDto;
import com.jeesite.modules.home.service.DcSupIndexService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.task.action.dao.DcSupTaskActionDao;
import com.jeesite.modules.task.action.entity.DcSupTaskAction;
import com.jeesite.modules.task.action.entity.DcSupTaskActionDto;
import com.jeesite.modules.task.action.service.DcSupTaskActionService;
import com.jeesite.modules.task.deploy.entity.DcSupTaskInfo;
import com.jeesite.modules.task.deploy.entity.OfficeUser;
import com.jeesite.modules.task.deploy.service.DcSupTaskInfoService;
import com.jeesite.modules.task.deploy.web.DcSupTaskInfoController;
import com.jeesite.modules.task.receive.entity.TaskReceiveDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @description: 督办首页
 * @author: xgq
 * @date: 2022/3/15 11:04
 */
@Controller
@RequestMapping(value = "${adminPath}/home/supIndex")
public class DcSupIndexController extends BaseController {

    @Autowired
    private DcSupIndexService dcSupIndexService;
    @Autowired
    private SysOfficeUnitService sysOfficeUnitService;
    @Autowired
    private DcSupTaskInfoService dcSupTaskInfoService;
    @Autowired
    private DcSupTaskActionService dcSupTaskActionService;
    @Autowired
    private DcSupTaskActionDao dcSupTaskActionDao;

    /**
     * 督办首页-待办已办
     * @param dcSupMsgDto
     * @return
     */
    @RequestMapping(value = "queryOne")
    @ResponseBody
    public Page<DcSupMsgDto> queryOne(DcSupMsgDto dcSupMsgDto,HttpServletRequest request,HttpServletResponse response){
        dcSupMsgDto.setPage(new Page<>(request,response));
        return dcSupIndexService.queryOne(dcSupMsgDto);
    }

    /**
     * 督办首页-任务进展
     * @return
     */
    @RequestMapping(value = "queryTwo")
    @ResponseBody
    public String queryTwo(DcSupTaskActionDto dcSupTaskActionDto) {
        List<EchartsDto> list = dcSupIndexService.queryTwo(dcSupTaskActionDto);
        Map<String,List> map = new HashMap<>();
        map.put("nameList",list.stream().map(e->e.getName()).collect(Collectors.toList()));
        map.put("valueList",list.stream().map(e->e.getValue()).collect(Collectors.toList()));
        map.put("list",list);
        return renderResult(Global.TRUE, text("任务进展查询成功"),map);
    }

    /**
     * 督办首页-任务预警信息
     * @return
     */
    @RequestMapping(value = "queryThree")
    @ResponseBody
    public List<DcSupWarningDto> queryThree(DcSupWarningDto dcSupWarningDto, HttpServletRequest request, HttpServletResponse response) {
//        dcSupWarningDto.setPage(new Page<>(request,response));
        return dcSupIndexService.queryThree(dcSupWarningDto);
    }

    /**
     * 督办首页-任务逾期信息详情
     * @return
     */
    @RequestMapping(value = "queryThreePage")
    @ResponseBody
    public Page<DcSupWarningDto> queryThreePage(DcSupWarningDto dcSupWarningDto,HttpServletRequest request,HttpServletResponse response) {
        dcSupWarningDto.setPage(new Page<>(request, response));
        return dcSupIndexService.queryThreePage(dcSupWarningDto);
    }

    /**
     * 督办首页-任务逾期信息详情
     * @return
     */
    @RequestMapping(value = "queryTwoPage")
    @ResponseBody
    public Page<DcSupTaskActionDto> queryTwoPage(DcSupTaskActionDto dcSupTaskActionDto, HttpServletRequest request, HttpServletResponse response) {
        dcSupTaskActionDto.setPage(new Page<>(request, response));
        Page<DcSupTaskActionDto> page = dcSupIndexService.queryTwoPage(dcSupTaskActionDto);
        if(dcSupTaskActionDto.getPageNo()>1&&page.getList().size()==0){
            dcSupTaskActionDto.setPageNo(1);
            page = dcSupIndexService.queryTwoPage(dcSupTaskActionDto);
        }
        return page;
    }

    /**
     * 督办首页-通知公告
     * @return
     */
    @RequestMapping(value = "queryFour")
    @ResponseBody
    public Page<DcSupMsgDto> queryFour(DcSupMsgDto dcSupMsgDto,HttpServletRequest request,HttpServletResponse response) {
        dcSupMsgDto.setPage(new Page<>(request, response));
        return dcSupIndexService.queryFour(dcSupMsgDto);
    }

    /**
     * 督办首页-逾期未反馈通报
     * @return
     */
    @RequestMapping(value = "queryFive")
    @ResponseBody
    public String queryFive(String taskYear,String flag) {
        List<EchartsDto> list = dcSupIndexService.queryFive(taskYear,flag);
        Map<String,List> map = new HashMap<>();
        map.put("nameList",list.stream().map(e->e.getName()).collect(Collectors.toList()));
        map.put("valueList",list.stream().map(e->e.getValue()).collect(Collectors.toList()));
        map.put("list",list);
        return renderResult(Global.TRUE, text("逾期未反馈通报查询成功"),map);
    }

    @RequestMapping(value = {"supIndexFive", ""})
    public String supIndexFive(TaskReceiveDto taskReceiveDto,Model model) {
        model.addAttribute("supIndexFive",taskReceiveDto);
        return "modules/home/supIndexFive";
    }

    @RequestMapping(value = {"supIndexFiveList"})
    @ResponseBody
    public Page<TaskReceiveDto> supIndexFiveList(TaskReceiveDto taskReceiveDto,HttpServletRequest request,HttpServletResponse response) {
        taskReceiveDto.setPage(new Page<>(request, response));
        taskReceiveDto.setCreateUnit(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
        return dcSupIndexService.supIndexFiveList(taskReceiveDto);
    }
    @RequestMapping(value = "supIndexFive2")
    public String taskReceiveFive(TaskReceiveDto taskReceive, Model model) {
        if(!isNumeric(taskReceive.getActionId()) || !isNumeric(taskReceive.getTaskId())){
            return "";
        }
        model.addAttribute("supIndexFive2", taskReceive);
        model.addAttribute("nowDate", new Date());
        return "modules/home/supIndexFive2";
    }

    /**
     * 督办首页-进度滞后通报
     * @return
     */
    @RequestMapping(value = "querySix")
    @ResponseBody
    public String querySix(String taskYear,String flag) {
        List<EchartsDto> list = dcSupIndexService.querySix(taskYear,flag);
        Map<String,List> map = new HashMap<>();
        map.put("nameList",list.stream().map(e->e.getName()).collect(Collectors.toList()));
        map.put("valueList",list.stream().map(e->e.getValue()).collect(Collectors.toList()));
        map.put("list",list);
        return renderResult(Global.TRUE, text("进度滞后查询成功"),map);
    }

    @RequestMapping(value = {"supIndexSix", ""})
    public String supIndexSix(TaskReceiveDto taskReceiveDto,Model model) {
        model.addAttribute("supIndexSix",taskReceiveDto);
        return "modules/home/supIndexSix";
    }

    @RequestMapping(value = {"supIndexTwoDetail", ""})
    public String supIndexTwoDetail(String name,DcSupTaskActionDto dcSupTaskActionDto,Model model) {
        model.addAttribute("supIndexTwoDetail",name);
        model.addAttribute("dcSupTaskActionDto",dcSupTaskActionDto);
        return "modules/home/supIndexTwoDetail";
    }

    @RequestMapping(value = {"supIndexSixList"})
    @ResponseBody
    public Page<TaskReceiveDto> supIndexSixList(TaskReceiveDto taskReceiveDto,HttpServletRequest request,HttpServletResponse response) {
        taskReceiveDto.setPage(new Page<>(request, response));
        taskReceiveDto.setCreateUnit(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
        return dcSupIndexService.supIndexSixList(taskReceiveDto);
    }

    /**
     * 工作门户 任务预警表格
     * @param dcSupWarningDto
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "queryGatewayWarning")
    @ResponseBody
    public Page<DcSupWarningDto> queryGatewayWarning(DcSupWarningDto dcSupWarningDto,HttpServletRequest request,HttpServletResponse response) {
        dcSupWarningDto.setPage(new Page<>(request,response));
        return dcSupIndexService.queryGatewayWarning(dcSupWarningDto);
    }

    /**
     * 工作门户 任务进展
     */
    @RequestMapping(value = "queryGatewayProgress")
    @ResponseBody
    public String queryGatewayprogress(){
        List<EchartsDto> list = dcSupIndexService.queryGatewayProgress();
        Map<String,List> map = new HashMap<>();
        map.put("nameList",list.stream().map(e->e.getName()).collect(Collectors.toList()));
        map.put("valueList",list.stream().map(e->e.getValue()).collect(Collectors.toList()));
        map.put("list",list);
      return renderResult(Global.TRUE, text("任务进展查询成功"),map);
    }

    /**
     * 督办首页-任务逾期信息详情
     * @return
     */
    @RequestMapping(value = "queryGatewayProgressPage")
    @ResponseBody
    public Page<DcSupTaskActionDto> queryGatewayProgressPage(DcSupTaskActionDto dcSupTaskActionDto, HttpServletRequest request, HttpServletResponse response) {
        dcSupTaskActionDto.setPage(new Page<>(request, response));
        return dcSupIndexService.queryGatewayProgressPage(dcSupTaskActionDto);
    }


    @RequestMapping(value = {"supIndexFiveDetail", ""})
    public String supIndexSix(Model model) {
        return "modules/home/supIndexFiveDetail";
    }


    @RequestMapping(value = {"supIndexThreeDetail", ""})
    public String supIndexThree(String taskYear,Model model) {
        model.addAttribute("queryThreePage",taskYear);
        return "modules/home/supIndexThreeDetail";
    }
    @RequestMapping(value = {"queryGatewayWarningDetail", ""})
    public String queryGatewayWarningDetail(Model model) {
        model.addAttribute("queryThreePage",null);
        return "modules/home/gatewayWarningDetail";
    }
    @RequestMapping(value = {"gatewayProgress", ""})
    public String gatewayProgress(String name ,Model model) {
        model.addAttribute("gatewayProgress",name);
        return "modules/home/gatewayProgress";
    }

    /**
     * 督办首页-总体督办事项数量
     * @return
     */
    @RequestMapping(value = "querySeven")
    @ResponseBody
    public String querySeven(String taskYear) {
        List<EchartsDto> list = dcSupIndexService.querySeven(taskYear);
        Map<String,List> map = new HashMap<>();
        map.put("list",list);
        return renderResult(Global.TRUE, text("总体督办事项数量查询成功"),map);
    }

    /**
     * 督办首页-单位承担督办事项TOP10排名
     * @return
     */
    @RequestMapping(value = "queryNine")
    @ResponseBody
    public String queryNine(String taskYear) {
        List<EchartsDto> list = dcSupIndexService.queryNine(taskYear);
        Map<String,List> map = new HashMap<>();
        map.put("list",list);
        return renderResult(Global.TRUE, text("单位承担督办事项TOP10排名查询成功"),map);
    }

    @RequestMapping(value = {"cuibanPage", ""})
    public String cuibanPage(cuibanDto cuiban , Model model) {
        // 获取任务信息
        DcSupTaskInfo dcSupTaskInfo = dcSupTaskInfoService.queryInfoByActionId(cuiban.getId());
        // 获取分发任务信息
        DcSupTaskAction dcSupTaskAction = dcSupTaskActionService.get(cuiban.getId());
        // 定义催办的初始文本
        cuiban.setMessage("您有一个任务:”" + dcSupTaskInfo.getTaskTitle() + ",请及时查看");

        // 定义临时变量
        String tempUserCode = "";
        String tempUserName = "";
        List<Map<String, String>> listUser = new ArrayList<>();
        // 判断分发任务是否转办
        if (StringUtils.isNotBlank(dcSupTaskAction.getTransferOrgCode()) && StringUtils.isNotBlank(dcSupTaskAction.getTransferOrgName())) {
            // 获取转办人信息
            User transferUser = UserUtils.get(dcSupTaskAction.getTransferOrgCode());
            // 放入转办人
            if (transferUser != null) {
                Map<String, String> map = new HashMap<>();
                map.put("name", transferUser.getUserName());
                map.put("userCode", transferUser.getUserCode());
                map.put("phone", transferUser.getMobile());
                map.put("id", transferUser.getUserCode());
                listUser.add(map);
                tempUserCode = transferUser.getUserCode();
                tempUserName = transferUser.getUserName();
            }
        } else {
            // 获取牵头责任单位的收文员
            List<OfficeUser> userByOrg = dcSupTaskActionDao.getUserByOrg("OTSTRANSFER", dcSupTaskAction.getLeaderOrgCode());
            if (userByOrg != null && userByOrg.size() > 0) {
                // 遍历集合，设置页面初始值
                for (int i = 0; i < userByOrg.size(); i++) {
                    User transferUser = UserUtils.get(userByOrg.get(i).getUserCode());
                    Map<String, String> map = new HashMap<>();
                    map.put("name", transferUser.getUserName());
                    map.put("userCode", transferUser.getUserCode());
                    map.put("phone", transferUser.getMobile());
                    map.put("id", transferUser.getUserCode());
                    listUser.add(map);
                    tempUserCode = tempUserCode + "," + transferUser.getUserCode();
                    tempUserName = tempUserName + "," + transferUser.getUserName();
                }
                // 过滤字符串第一个逗号
                tempUserCode = tempUserCode.substring(1);
                tempUserName = tempUserName.substring(1);
            }
        }

        model.addAttribute("defaultUserList", listUser);
        cuiban.setUserCode(tempUserCode);
        cuiban.setName(tempUserName);
        model.addAttribute("cuibanPage", cuiban);
        model.addAttribute("orgCode", dcSupTaskAction.getLeaderOrgCode());
        return "modules/home/cuibanPage";
    }

    /**
     * 评分汇总催办
     * @param cuiban
     * @param model
     * @return
     */
    @RequestMapping(value = {"cuibanPage2", ""})
    public String cuibanPage2(cuibanDto cuiban , Model model) {

        // 定义催办的初始文本
        cuiban.setMessage("您有一个评分被催办,请及时查看");

        // 定义临时变量
        String tempUserCode = "";
        String tempUserName = "";
        List<Map<String, String>> listUser = new ArrayList<>();

            // 获取考核单位角色
            List<OfficeUser> userByOrg = dcSupTaskActionDao.getUserByOrg("EL_ORG", cuiban.getOrg());
            if (userByOrg != null && userByOrg.size() > 0) {
                // 遍历集合，设置页面初始值
                for (int i = 0; i < userByOrg.size(); i++) {
                    User transferUser = UserUtils.get(userByOrg.get(i).getUserCode());
                    Map<String, String> map = new HashMap<>();
                    map.put("name", transferUser.getUserName());
                    map.put("userCode", transferUser.getUserCode());
                    map.put("phone", transferUser.getMobile());
                    map.put("id", transferUser.getUserCode());
                    listUser.add(map);
                    tempUserCode = tempUserCode + "," + transferUser.getUserCode();
                    tempUserName = tempUserName + "," + transferUser.getUserName();
                }
                // 过滤字符串第一个逗号
                tempUserCode = tempUserCode.substring(1);
                tempUserName = tempUserName.substring(1);
            }

        model.addAttribute("defaultUserList", listUser);
        cuiban.setUserCode(tempUserCode);
        cuiban.setName(tempUserName);
        model.addAttribute("cuibanPage", cuiban);
        model.addAttribute("orgCode", cuiban.getOrg());
        return "modules/home/cuibanPage2";
    }

    @RequestMapping(value = "cuiban")
    @ResponseBody
    @RepeatSubmit
    public String cuiban(cuibanDto cuiban) {
        System.out.println(cuiban.toString());
        dcSupIndexService.cuiban(cuiban);
        return renderResult(Global.TRUE, text("催办成功!"));
    }

    @RequestMapping(value = "cuiban2")
    @ResponseBody
    @RepeatSubmit
    public String cuiban2(cuibanDto cuiban) {
        System.out.println(cuiban.toString());
        dcSupIndexService.cuiban2(cuiban);
        return renderResult(Global.TRUE, text("催办成功!"));
    }

    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    /**
     * 单位承办督办事项TOP10排名
     */
    @RequestMapping(value = "queryOrgRank")
    @ResponseBody
    public Object  queryOrgRank(){
        List<Map> list = dcSupIndexService.queryOrgRank();
        return renderResult(Global.TRUE, text("单位承办督办事项TOP10排名查询成功"),list);
    }

}
